<template>
  <div>
    <el-dialog
      center
      :title="isAdd ? '添加管理员' : '编辑管理员'"
      :visible.sync="isShow"
      :close-on-press-escape="false"
      :before-close="cancel"
    >
      <el-form :model="user_info" :rules="rules" ref="userinfo">
        <el-form-item
          prop="roleid"
          label="所属角色"
          :label-width="formLabelWidth"
        >
          <el-select v-model="user_info.roleid" placeholder="请选择">
            <el-option
              v-for="item in rolelist"
              :key="item.id"
              :value="item.id"
              :label="item.rolename"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          prop="username"
          label="管理员名称"
          :label-width="formLabelWidth"
        >
          <el-input v-model="user_info.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码" :label-width="formLabelWidth">
          <el-input v-model="user_info.password" autocomplete="off"></el-input>
        </el-form-item>
        <p>密码为空，则不做修改</p>
        <el-form-item label="状态" :label-width="formLabelWidth">
          <el-switch
            v-model="user_info.status"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="1"
            :inactive-value="2"
          >
          </el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="comfirm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { useradd, userinfo, useredit } from "../../request/api";
//引入辅助性函数
import { mapActions, mapGetters } from "vuex";
export default {
  props: ["isShow", "isAdd"],
  data() {
    return {
      user_info: {
        roleid: "", //角色编号
        username: "", //管理员名称
        password: "", //密码
        status: 1, //状态   1正常2禁用   number类型
      },
      formLabelWidth: "120px",
      rules: {
        username: [
          { required: true, message: "请输入管理员名称", trigger: "blur" },
          {
            min: 2,
            max: 15,
            message: "长度在 2 到 15 个字符",
            trigger: "blur",
          },
        ],
        roleid: [
          { required: true, message: "请选择上级管理员", trigger: "change" },
        ],
      },
    };
  },
  mounted() {
    //触发角色行动
    if (this.rolelist.length == 0) {
      this.getRoleAction();
    }
  },
  computed: {
    ...mapGetters({
      rolelist: "role/getRole",
    }),
  },
  methods: {
    ...mapActions({
      getRoleAction: "role/getRoleAction",
      getCount: "user/getCountAction",
      getUserList: "user/getUserAction",
    }),
    //封装一个取消事件
    cancel() {
      this.$emit("cancel", false);
      //并清空
      this.reset();
    },
    //封装一个重置事件
    reset() {
      this.user_info = {
        roleid: "", //角色编号
        username: "", //管理员名称
        password: "", //密码
        status: 1, //状态   1正常2禁用   number类型
      };
      //清空表单验证
      this.$refs.userinfo.resetFields();
    },
    //封装一个函数用来数据回显
    lookup(uid) {
      userinfo({
        uid,
      }).then((res) => {
        console.log(res, "获取一条数据");
        this.user_info = res.list;
        this.user_info.uid = uid;
        //因为后端要求修改的时候密码为空则不做修改，否则则修改最新的值
        this.user_info.password = "";
      });
    },
    //封装一个确定事件
    comfirm() {
      this.$refs.userinfo.validate((val) => {
        if (val) {
          //判断是否为添加还是编辑
          if (this.isAdd) {
            //添加接口
            useradd(this.user_info).then((res) => {
              // console.log(res, "响应");
              if (res.code == 200) {
                //添加成功
                this.$message.success(res.msg);
                //重新获取列表
                this.getUserList();
                //重新获取总条数
                this.getCount();
                //关闭弹框
                this.cancel();
              } else {
                this.$message.error(res.msg);
              }
            });
          } else {
            //编辑接口
            useredit(this.user_info).then((res) => {
              if (res.code == 200) {
                //添加成功
                this.$message.success(res.msg);
                this.cancel();
                //重新获取列表
                this.getUserList();
                //重新获取总条数
                this.getCount();
              } else {
                this.$message.error(res.msg);
              }
            });
          }
        }
      });
    },
  },
};
</script>

<style scoped></style>
